import {defHttp} from '/@/utils/http/axios';
import {Modal} from 'ant-design-vue';

enum Api {
  list = '/system-service/role/page',
  save = '/system-service/role/save',
  edit = '/system-service/role/update',
  deleteRole = '/system-service/role/delete',
  deleteBatch = '/system-service/role/deleteBatch',
  exportXls = '/system-service/role/exportXls',
  importExcel = '/system-service/role/importExcel',
  isRoleExist = '/system-service/role/checkRoleCode',
  listDeptTreeForRole = '/system-service/dept/listDeptTreeForRole',
  listDeptInRole = '/system-service/dept/listDeptInRole',
  queryRolePermission = '/system-service/role/listRolePermission',
  saveRolePermission = '/system-service/role/addRolePermission',
  updateDataScope = '/system-service/role/updateDataScope',
  queryDataRule = '/system-service/role/listDataRule',
  saveDataRule = '/system-service/role/saveDataRule',
  getParentDesignList = '/act/process/extActDesignFlowData/getDesFormFlows',
  getRoleDegisnList = '/joa/designform/designFormCommuse/getRoleDegisnList',
  saveRoleDesign = '/joa/designform/designFormCommuse/sysRoleDesignAdd',
  userList = '/system-service/tenantMember/listRoleUser',
  deleteUserRole = '/system-service/role/deleteUser',
  batchDeleteUserRole = '/system-service/role/deleteBatchUser',
  addUserRole = '/system-service/role/addUser',

  listApiResource = '/system-service/role/listApiResource',
  saveApiResource = '/system-service/role/saveApiResource',
}
/**
 * 导出api
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

export const list = (params) =>
  defHttp.get({url: Api.list, params});


export const deleteRole = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteRole, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteRole = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const saveOrUpdateRole = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

export const isRoleExist = (params) =>
  defHttp.get({url: Api.isRoleExist, params}, {isTransformResponse: false});


 export const listDeptTreeForRole = () =>
 defHttp.get({url: Api.listDeptTreeForRole});


export const listDeptInRole = (params) => defHttp.get({url: Api.listDeptInRole, params});

export const queryRolePermission = (params) =>
  defHttp.get({url: Api.queryRolePermission, params});

export const saveRolePermission = (params) =>
  defHttp.post({url: Api.saveRolePermission, params});

export const updateDataScope = (params) => defHttp.post({url: Api.updateDataScope, params});

export const queryDataRule = (params) =>
  defHttp.get({url: Api.queryDataRule, params: {
    permissionId: params.functionId,
    roleId: params.roleId
  }}, {isTransformResponse: true});

export const saveDataRule = (params) => defHttp.post({url: Api.saveDataRule,params});

export const getParentDesignList = () => defHttp.get({url: Api.getParentDesignList});

export const getRoleDegisnList = (params) => defHttp.get({url: Api.getRoleDegisnList,params});
export const saveRoleDesign = (params) => defHttp.post({url: Api.saveRoleDesign,params});

export const userList = (params) => defHttp.get({url: Api.userList, params});

export const deleteUserRole = (params, handleSuccess) => {
  return defHttp.delete({url: Api.deleteUserRole, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

export const batchDeleteUserRole = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.batchDeleteUserRole, params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

export const addUserRole = (params, handleSuccess) => {
  return defHttp.post({url: Api.addUserRole, params}).then(() => {
    handleSuccess();
  });
}

export const listApiResource = (params) => {
  return defHttp.get({url: Api.listApiResource, params});
}

export const saveApiResource = (params) => {
  return defHttp.post({url: Api.saveApiResource, params});
}